var unk = "*";

function isArray (obj) {
  return (obj.constructor == Array);
}

function toArray (obj) {
var res = new Array();

  if (isArray(obj)) {
    res = obj;
  } else {
    res.push(obj);
  }
  return res;
}

function url_xml_esc (s) {
  s = s.replace(/&amp;|&/igm,escape("&"));
  s = s.replace(/&apos;|'/igm,escape("'"));
  s = s.replace(/&quot;|"/igm,escape("\""));
  s = s.replace(/&lt;|</igm,escape("<"));
  s = s.replace(/&gt;|>/igm,escape(">"));
  return s;
}

function xml_esc (s) {
  s = s.replace(/</igm,escape("<"));
  s = s.replace(/>/igm,escape(">"));
  return s;
}

function findHostByName (tname, obj) {
  var res = null;
  var t = null;

  for (var i = 0; i < obj.host.length; i++) {
    t = obj.host[i];
    if (t.name == tname) {
      res = t;
      break;
    }
  }
  return res;
}

function findParmByName (tname, obj) {
  var res = null;
  var t = null;

  for (var i = 0; i < obj.parm.length; i++) {
    t = obj.parm[i];
    if (t.name == tname) {
      res = t;
      break;
    }
  }
  return res;
}

function findHostIndexByName (tname, obj) {
  var res = null;
  var t = null;

  for (var i = 0; i < obj.host.length; i++) {
    t = obj.host[i];
    if (t.name == tname) {
      res = i;
      break;
    }
  }
  return res;
}

function getNameOrTxt (obj) {
  var res = null;

  if (obj.txt) {
    res = obj.txt;
  } else if (obj.name) {
    res = obj.name;
  }
  return res;
}

function getHrefOrUrl (obj) {
  var res = "#";

  if (obj.href) {
    res = obj.href;
  } else if (obj.url) {
    res = obj.url;
  }
  return res;
}

function getValue (obj) {
  var res = unk;

  if (obj.value)
    res = obj.value;
  return res;
}

function getCheckHref (obj) {
  var res = obj.href;

  if (obj.check) {
    var checkArray = toArray(obj.check);
    for (i = checkArray.length - 1; i >= 0; i--) {
      var t = findParmByName(checkArray[i], obj);
      if (t && t.zero != null && t.value != null && t.value != t.zero && t.value != unk) {
	res = t.href;
      }
    }
  }
  return res;
}

function sortByName(a, b) {
    var x = a.name.toLowerCase();
    var y = b.name.toLowerCase();
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}